import random
2
from math import sin, cos, pi, log
3
from tkinter import *
4 
5
CANVAS_WIDTH = 660  # 画布的宽
6
CANVAS_HEIGHT = 500  # 画布的高
7
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
8
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标
9
IMAGE_ENLARGE = 14  # 放大比例
10
HEART_COLOR = "#990099"  # 心的颜色，这个是中国红
11 
12 
13
def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
14    
"""
15
    “爱心函数生成器”
16
    :param shrink_ratio: 放大比例
17
    :param t: 参数
18
    :return: 坐标
19
    """
20    
# 基础函数
21
    x = 16 * (sin(t) ** 3)
22
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
23 
24    
# 放大
25
    x *= shrink_ratio
26
    y *= shrink_ratio
27 
28    
# 移到画布中央
29
    x += CANVAS_CENTER_X
30
    y += CANVAS_CENTER_Y
31 
32    
return int(x), int(y)
33 
34 
35
def scatter_inside(x, y, beta=0.15):
36    
"""
37
    随机内部扩散
38
    :param x: 原x
39
    :param y: 原y
40
    :param beta: 强度
41
    :return: 新坐标
42
    """
43
    ratio_x = - beta * log(random.random())
44
    ratio_y = - beta * log(random.random())
45 
46
    dx = ratio_x * (x - CANVAS_CENTER_X)
47
    dy = ratio_y * (y - CANVAS_CENTER_Y)
48 
49    
return x - dx, y - dy
50 
51 
52
def shrink(x, y, ratio):
53    
"""
54
    抖动
55
    :param x: 原x
56
    :param y: 原y
57
    :param ratio: 比例
58
    :return: 新坐标
59
    """
60
    force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...
61
    dx = ratio * force * (x - CANVAS_CENTER_X)
62
    dy = ratio * force * (y - CANVAS_CENTER_Y)
63    
return x - dx, y - dy
64 
65 
66
def curve(p):
67    
"""
68
    自定义曲线函数，调整跳动周期
69
    :param p: 参数
70
    :return: 正弦
71
    """
72    
# 可以尝试换其他的动态函数，达到更有力量的效果（贝塞尔？）
73    
return 2 * (2 * sin(4 * p)) / (2 * pi)
74 
75 
76
class Heart:
77    
"""
78
    爱心类
79
    """
80 
81    
def __init__(self, generate_frame=20):
82
        self._points = set()  # 原始爱心坐标集合
83
        self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合
84
        self._center_diffusion_points = set()  # 中心扩散效果点坐标集合
85
        self.all_points = {}  # 每帧动态点坐标
86
        self.build(2000)
87 
88
        self.random_halo = 1000
89 
90
        self.generate_frame = generate_frame
91        
for frame in range(generate_frame):
92
            self.calc(frame)
93 
94    
def build(self, number):
95        
# 爱心
96        
for _ in range(number):
97
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
98
            x, y = heart_function(t)
99
            self._points.add((x, y))
100 
101        
# 爱心内扩散
102        
for _x, _y in list(self._points):
103            
for _ in range(3):
104
                x, y = scatter_inside(_x, _y, 0.05)
105
                self._edge_diffusion_points.add((x, y))
106 
107        
# 爱心内再次扩散
108
        point_list = list(self._points)
109        
for _ in range(4000):
110
            x, y = random.choice(point_list)
111
            x, y = scatter_inside(x, y, 0.17)
112
            self._center_diffusion_points.add((x, y))
113 
114
    @staticmethod
115    
def calc_position(x, y, ratio):
116        
# 调整缩放比例
117
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)  # 魔法参数
118 
119
        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
120
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
121 
122        
return x - dx, y - dy
123 
124    
def calc(self, generate_frame):
125
        ratio = 10 * curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例
126 
127
        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
128
        halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
129 
130
        all_points = []
131 
132        
# 光环
133
        heart_halo_point = set()  # 光环的点坐标集合
134        
for _ in range(halo_number):
135
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
136
            x, y = heart_function(t, shrink_ratio=11.6)  # 魔法参数
137
            x, y = shrink(x, y, halo_radius)
138            
if (x, y) not in heart_halo_point:
139                
# 处理新的点
140
                heart_halo_point.add((x, y))
141
                x += random.randint(-14, 14)
142
                y += random.randint(-14, 14)
143
                size = random.choice((1, 2, 2))
144
                all_points.append((x, y, size))
145 
146        
# 轮廓
147        
for x, y in self._points:
148
            x, y = self.calc_position(x, y, ratio)
149
            size = random.randint(1, 3)
150
            all_points.append((x, y, size))
151 
152        
# 内容
153        
for x, y in self._edge_diffusion_points:
154
            x, y = self.calc_position(x, y, ratio)
155
            size = random.randint(1, 2)
156
            all_points.append((x, y, size))
157 
158        
for x, y in self._center_diffusion_points:
159
            x, y = self.calc_position(x, y, ratio)
160
            size = random.randint(1, 2)
161
            all_points.append((x, y, size))
162 
163
        self.all_points[generate_frame] = all_points
164 
165    
def render(self, render_canvas, render_frame):
166        
for x, y, size in self.all_points[render_frame % self.generate_frame]:
167
            render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
168 
169 
170
def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
171
    render_canvas.delete('all')
172
    render_heart.render(render_canvas, render_frame)
173
    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
174 
175 
176
if __name__ == '__main__':
177
    root = Tk()  # 一个Tk
178
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
179
    canvas.pack()
180
    heart = Heart()  # 心
181
    draw(root, canvas, heart)  # 开始画画~
182
    root.mainloop()
183